package demo.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import demo.dao.IProductDao;
import demo.dto.ProductDTO;
import demo.model.Product;

@Controller
@RequestMapping("product")
public class ProductController {

	@Resource
	private IProductDao productDao; 
	
	
	
	@RequestMapping("findAll.do")
	@ResponseBody
	public Map findAll(ProductDTO dto){
		Map map = new HashMap();
		map.put("code", "0");
		map.put("msg", "");
		map.put("count", productDao.getCount(dto));
		map.put("data", productDao.findAll(dto));
		return map;
	}
	
	//��ӻ��޸�
	@RequestMapping("/addOrEdit.do")
	@ResponseBody
	public int addOrEdit(Product p) {
		if(p.getId()==null) {
			return productDao.add(p);
		}else {
			return productDao.edit(p);
		}
	}
	
	//���¼�
	@RequestMapping("/putOn.do")
	@ResponseBody
	public int putOn(int id,String isPutOnSale) {
		return productDao.isPutOn(id, isPutOnSale);
	}
	
	//�Ƽ�/ȡ���Ƽ�
	@RequestMapping("/recommend.do")
	@ResponseBody
	public int recommend(int id,String isToday) {
		return productDao.recommend(id, isToday);
	}
	
	
	//�ϴ�ͼƬ
	@RequestMapping("/upload.do")
	@ResponseBody
	public Map upload(MultipartFile file) {
		//��ȡ�ļ�ԭʼ��
		String name = file.getOriginalFilename();
		System.out.println(name); 
		String path = "d:\\upload\\"+name;
		try {
			file.transferTo(new File(path));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data",name);
		return map;
	}
	
	//�޸�ͼƬ·��
	@RequestMapping("/updateUrl.do")
	@ResponseBody
	public int updateUrl(int id,String url) {
		return productDao.updateUrl(id, url);
	}
	
	@RequestMapping("findToday.do")
	public List<Product> findTOday(){
		return productDao.findToday();
	}
	
	
	@RequestMapping("findById.do")
	public Product findByid(Integer id){
		return productDao.findById(id);
	}
	
}
